package com.smedi.ismedi.constructionproject.core.port.adapter.restful;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smedi.ismedi.constructionproject.api.dto.request.BidSectionRequest;
import com.smedi.ismedi.constructionproject.api.dto.response.BidSectionResponse;
import com.smedi.ismedi.constructionproject.api.dto.response.BidSectionViewResponse;
import com.smedi.ismedi.constructionproject.api.feign.RemoteBidSectionFeignApi;
import com.smedi.ismedi.constructionproject.core.application.command.BidSectionCommand;
import com.smedi.ismedi.constructionproject.core.application.query.BidSectionQueryService;
import com.smedi.ismedi.constructionproject.core.application.query.vo.BidSectionVo;
import com.smedi.ismedi.constructionproject.core.application.service.BidSectionApplicationService;
import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.BidSection;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-6-21 10:16
 */
@Slf4j
@RestController
@AllArgsConstructor
public class BidSectionController implements RemoteBidSectionFeignApi {
    private final DozerUtil dozerUtil;

    private final BidSectionApplicationService bidSectionApplicationService;

    private final BidSectionQueryService bidSectionQueryService;

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<BidSectionResponse> postSaveBidSection(BidSectionRequest bidSectionRequest) {
        BidSectionCommand bidSectionCommand = dozerUtil.convertor(bidSectionRequest, BidSectionCommand.class);
        BidSection bidSection = bidSectionApplicationService.saveBidSectionInfo(bidSectionCommand);
        return new ResponseData<BidSectionResponse>().success(dozerUtil.convertor(bidSection, BidSectionResponse.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<PaginationData> getQueryBidSectionPagination(Long offset, Long pageSize, Long projectId) {
        IPage pagination = bidSectionQueryService.getBidSectionPaginationByProjectId(offset, pageSize, projectId);
        pagination.setRecords(dozerUtil.convertor(pagination.getRecords(), BidSectionViewResponse.class));
        PaginationData paginationResponse = dozerUtil.convertor(pagination, PaginationData.class);
        return new ResponseData<PaginationData>().success(paginationResponse);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<BidSectionViewResponse> getQueryBidSectionView(Long bidSectionId) {
        BidSectionVo bidSectionVo = bidSectionQueryService.getBidSectionByBidSectionId(bidSectionId);
        return new ResponseData<BidSectionViewResponse>().success(dozerUtil.convertor(bidSectionVo, BidSectionViewResponse.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> deleteBidSection(Long bidSectionId) {
        bidSectionApplicationService.deleteBidSection(bidSectionId);
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> getCheckProjectOrganizationBeUsedInBidSection(Long projectOrganizationId) {
        return new ResponseData<Boolean>().success(bidSectionApplicationService.checkProjectOrgBeUsedInBidSection(projectOrganizationId));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<BidSectionResponse>> getBidSectionList(Long projectId) {
        List<BidSection> list = bidSectionQueryService.getBidSectionListByProjectId(projectId);

        return new ResponseData<List<BidSectionResponse>>().success(dozerUtil.convertor(list, BidSectionResponse.class));
    }
}
